package com.honeycomb.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 当前时间， 只记录 yyyy dd mm HH mm ss 格式，毫秒都舍去。
 * @author user
 *
 */
public class MyDateDTTM {
	
	private Integer dateValue;
	
	public MyDateDTTM (Integer dateValue) {
		this.dateValue = dateValue;
	}
	
	public MyDateDTTM() {
		this.dateValue = convertCurrentDTTMtoInt();
	}
	
	public Integer value() {
		return dateValue;
	}
	
	// 格式化时间yyyy-MM-dd HH:mm:ss
	public String formatToDTTMString() {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_NORMAL);
		return df.format(convertIntToDTTM(dateValue));
	}

	// 格式化时间yyyy-MM-dd
	public String formatDTString() {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_SHORT_DT);
		return df.format(convertIntToDTTM(dateValue));
	}

	// 格式化时间HH:mm:ss
	public String formatTMString() {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_SHORT_TM);
		return df.format(convertIntToDTTM(dateValue));
	}
	
	public static MyDateDTTM decodeFromDTString(String dtStr) {
		return new MyDateDTTM(convertDTTMtoInt(formatStringToDateDT(dtStr)) );
	}
	
	public static MyDateDTTM decodeFromDTTMString(String dttmStr) {
		return new MyDateDTTM(convertDTTMtoInt(formatStringToDateDTTM(dttmStr)) );
	}
	
	// 得到指定时间Date的INT值
	public static final int convertDTTMtoInt(Date givenDTTM) {
		return (int) (givenDTTM.getTime() / 1000);
	}

	// 得到当前时间Date的INT值
	public static final int convertCurrentDTTMtoInt() {
		return convertDTTMtoInt(new Date());
	}

	// 得到INT时间对应的时间Date
	public static final Date convertIntToDTTM(int dttmInt) {
		return new Date((long) dttmInt * 1000);
	}

	// 得到指定日期Date的INT值
	public static final int convertDTtoInt(Date givenDTTM) {
		Calendar calen = Calendar.getInstance();
		calen.setTime(givenDTTM);
		int hours = calen.get(Calendar.HOUR_OF_DAY) + 8;
		calen.set(Calendar.HOUR_OF_DAY, hours);
		return (int) (calen.getTimeInMillis() / 1000 / 60 / 60 / 24);
	}

	// 得到当前日期Date的INT值
	public static final int convertCurrentDTtoInt() {
		return convertDTtoInt(new Date());
	}

	// 得到INT日期对应的时间Date
	public static final Date convertIntToDT(int dttmInt) {
		return new Date((long) dttmInt * 1000 * 60 * 60 * 24);
	}

	// 格式化时间yyyy-MM-dd HH:mm:ss
	public static final String formatDTTM(Date dt) {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_NORMAL);
		return df.format(dt);
	}

	// 格式化时间yyyy-MM-dd
	public static final String formatDT(Date dt) {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_SHORT_DT);
		return df.format(dt);
	}

	// 格式化时间HH:mm:ss
	public static final String formatTM(Date dt) {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_SHORT_TM);
		return df.format(dt);
	}

	// 把yyyy-MM-dd HH:mm:ss类型的String格式化为Date
	public static final Date formatStringToDateDTTM(String dtStr) {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_NORMAL);
		try {
			return df.parse(dtStr);
		} catch (ParseException e) {
			throw new IllegalArgumentException("Input string " + dtStr + " can not be parsed to Date correctly.", e);
		}
	}

	// 把yyyyMMdd类型的String格式化为Date
	public static final Date formatStringToDateDT(String dtStr) {
		DateFormat df = new SimpleDateFormat(SystemConstants.DATE_PATTERN_SHORT_DT);
		try {
			return df.parse(dtStr);
		} catch (ParseException e) {
			throw new IllegalArgumentException("Input string " + dtStr + " can not be parsed to Date correctly.", e);
		}
	}
	
	// 得到当前月的第一天的Int值
	public static final int getCurrentFirstDTInt() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar .YEAR);
		int month = calendar.get(Calendar .MONTH)+1;
		return getMonthFirstDTInt(year, month);
	}
	
	// 得到当前月的最后一天的Int值
	public static final int getCurrentLastDTInt() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar .YEAR);
		int month = calendar.get(Calendar .MONTH)+1;
		return getMonthLastDTInt(year, month);
	}
	
	// 得到当前月的第一天的Int值
	public static final int getMonthFirstDTInt(int year, int month) {
		return convertDTtoInt(formatStringToDateDT(year+"-"+month+"-01"));
	}
	
	// 得到当前月的最后一天的Int值
	public static final int getMonthLastDTInt(int year, int month) {
		return convertDTtoInt(formatStringToDateDT(year+"-"+(month+1)+"-01"))-1;
	}


}
